import json

from PyQt5.QtWidgets import (
    QHBoxLayout,
    QVBoxLayout,
    QLabel,
    QLineEdit,
    QPushButton,
    QWidget,
    QFileDialog,
    QComboBox,
    QMessageBox,
)
from PyQt5.QtCore import pyqtSignal, pyqtSlot
from pydantic import BaseModel, Field, field_validator
from ..basic_grid_component import BasicGridComponent

# import gui.controls.envs as envs
from ..dlg_model_params import DlgModelParams
from ..utils import Utils
from ..util import util_key_map


from enum import Enum


class StrEnumWithAlias(str, Enum):
    def __new__(cls, value, alias):
        obj = str.__new__(cls, value)
        obj._value_ = value
        obj.alias = alias
        return obj


class damageXEnum(StrEnumWithAlias):
    t1 = "损伤示意图", "损伤示意图"


class RepairPlanEnum(StrEnumWithAlias):
    t1 = "修理方案截面示意图", "修理方案截面示意图"


class IntactModelEnum(StrEnumWithAlias):
    t1 = "应力", "应力"
    t2 = "应变", "应变"
    t3 = "位移", "位移"


class AnalysisResultsEnum(StrEnumWithAlias):
    t1 = "应力", "应力"
    t2 = "应变", "应变"
    t3 = "位移", "位移"


class RepairAnalysisResultsEnum(StrEnumWithAlias):
    t1 = "应力", "应力"
    t2 = "应变", "应变"
    t3 = "位移", "位移"


# 定义 Pydantic 模型
class PostProcessingData(BaseModel):

    damageX: damageXEnum = Field(
        description="损伤X",
        alias="damageX",
        required=True,
    )
    damageXRepairPlan: RepairPlanEnum = Field(
        description="损伤X对应维修方案",
        alias="damageXRepairPlan",
        required=True,
    )
    intactModelAnalysisResults: IntactModelEnum = Field(
        description="无损模型分析结果",
        alias="intactModelAnalysisResults",
        required=True,
    )
    damageXAnalysisResults: AnalysisResultsEnum = Field(
        description="损伤X分析结果",
        alias="damageXAnalysisResults",
        required=True,
    )
    damageXRepairAnalysisResults: RepairAnalysisResultsEnum = Field(
        description="损伤X对应维修方案分析结果",
        alias="damageXRepairAnalysisResults",
        required=True,
    )


class PostProcessingComponent(BasicGridComponent):
    detailParams_modelShow_signal = pyqtSignal(object)

    def __init__(self, parent=None):
        super().__init__(parent)

    def get_model(self):
        return PostProcessingData

    def updateUi(self, data):
        data = util_key_map.convertKey(data, 0)
        for k, v in data.items():
            if isinstance(self.gridWidgets[k], QComboBox):
                self.gridWidgets[k].setCurrentText(v)
            else:
                self.gridWidgets[k].setValue(v)


class DlgPostProcessing(DlgModelParams):
    postProcessing_save_signal = pyqtSignal(object)

    def __init__(self, parent=None):
        super(DlgPostProcessing, self).__init__(parent)
        self.component = PostProcessingComponent()
        self.setWindowTitle("后处理")
        self.detailParams_modelShow_signal = (
            self.component.detailParams_modelShow_signal
        )
        self.vbox.insertWidget(0, self.component)
        # data = Utils.read_json("data/ipimas/uis/PostProcessing.json")
        # self.updateUi(data)

    def init(self, data):
        self.updateUi(data)

    @pyqtSlot()
    def on_pBtnOK_clicked(self):
        # res = super().on_pBtnOK_clicked()
        valueRes = self.component.getValues()
        descriptionRes = self.component.getValuesUseDescription()
        file = "data/ipimas/uis/PostProcessing.json"
        data = util_key_map.convertKey(valueRes, 1)
        Utils.write_json(file, data)
        self.close()
        # if res:
        #     self.close()
        #     if 'damageX' in res:
        #         res['damageX'] = res['damageX'].value
        #     if 'damageXRepairPlan' in res:
        #         res['damageXRepairPlan'] = res['damageXRepairPlan'].value
        #     if 'intactModelAnalysisResults' in res:
        #         res['intactModelAnalysisResults'] = res['intactModelAnalysisResults'].value
        #     if 'intactModelAnalysisResults' in res:
        #         res['damageXAnalysisResults'] = res['damageXAnalysisResults'].value
        #     if 'damageXRepairAnalysisResults' in res:
        #         res['damageXRepairAnalysisResults'] = res['damageXRepairAnalysisResults'].value
        self.postProcessing_save_signal.emit(data)

    def closeEvent(self, event):
        DlgPostProcessing._shown = False  # 使用类名访问类属性
        DlgPostProcessing._instance = None  # 清除实例引用
        event.accept()
